import React from 'react';
import styles from '@/pages/Organization/EmployeeManage/components/EmployeeCard/index.less';
import IconFont from '@/components/IconFont';
import { message, Modal, Popover } from 'antd';
import { useRequest } from '@/hooks';
import { accountForget } from '@/services/user';
import { eventTrack } from '@/utils/track';

/**
 * 员工信息头部 - 重置密码
 */
const ControlsItemRestPassword = ({ employeeInfo = {}, children, ...rest }) => {
    const { run: restPassword } = useRequest(accountForget, {
        manual: true
    });

    const handleSuccessModal = () => {
        Modal.info({
            title: '重置密码成功',
            content: (
                <div style={{ marginTop: '20px' }}>
                    <p>新密码已发送到员工邮箱</p>
                </div>
            ),
            okText: '确定',
            centered: true,
            maskClosable: true
        });
    };

    const handleResetModal = () => {
        eventTrack({
            eventName: 'personnel_management_organization_employee_list_operator_click',
            pageName: 'personnel_management_organization',
            eventProps: {
                click_context: `${employeeInfo.employeeName}-重置密码`
            }
        });
        Modal.confirm({
            title: `确定要重置员工 ${employeeInfo.employeeName} 的密码吗？`,
            content: '重置后，系统将随机自动生成新密码并发送到该员工的企业邮箱，原有密码失效！',
            okText: '确定',
            cancelText: '取消',
            centered: true,
            onOk: () =>
                new Promise((resolve, reject) => {
                    restPassword({
                        loginName: employeeInfo.email,
                        organizeType: 1,
                        name: employeeInfo.employeeName
                    }).then(({ code, msg }) => {
                        if (code === 10000) {
                            // eventTrack({
                            //     eventName: 'organization_staff_detail_password_reset_submit_click',
                            //     eventProps: { label: '重置密码-确定', status: 0 }
                            // });
                            handleSuccessModal();
                            resolve();
                        } else {
                            // eventTrack({
                            //     eventName: 'organization_staff_detail_password_reset_submit_click',
                            //     eventProps: { label: '重置密码-确定', status: 1, error_info: res.msg }
                            // });
                            message.error(msg);
                            reject();
                        }
                    });
                })
        });
    };

    // render
    return (
        <>
            {children ? (
                <span onClick={handleResetModal} {...rest}>
                    {children}
                </span>
            ) : (
                <Popover content="重置密码">
                    <div className={styles.employeeControlsItem} onClick={handleResetModal} {...rest}>
                        <IconFont icon="iconlistfunction_pwdreset_icon_default" />
                    </div>
                </Popover>
            )}
        </>
    );
};

export default ControlsItemRestPassword;
